@PACKAGE_INIT@

set_and_check(TMAC_LIB_DIR "@PACKAGE_TMAC_LIB_INSTALL_DIR@")
set_and_check(TMAC_INCLUDE_DIRS "@PACKAGE_TMAC_INCLUDE_INSTALL_DIR@")

find_library(
  tvm_runtime_LIBRARY tvm_runtime
  HINTS ${TMAC_LIB_DIR}
)

find_file(
  kernels_LIBRARY kernels.dll
  HINTS ${TMAC_LIB_DIR}
)

find_file(
  kernels_OBJECT kernels.o
  HINTS ${TMAC_LIB_DIR}
)

find_file(
  tuned_kcfg kcfg.ini
  REQUIRED
  HINTS ${TMAC_LIB_DIR}
)

find_file(
  kernels_source kernels.cc
  REQUIRED
  HINTS ${TMAC_LIB_DIR}
)

set(TMAC_COMPILE_DEFS TMAC_KCFG_FILE=${tuned_kcfg})

if(kernels_OBJECT)
  set(TMAC_OBJECT_COMPILE_DEFS ${TMAC_COMPILE_DEFS} TMAC_USE_SYSLIB=1 @TMAC_TVM_COMPILE_DEFS@)
  add_library(t_mac_object OBJECT IMPORTED)
  set_target_properties(
    t_mac_object
    PROPERTIES
      INTERFACE_INCLUDE_DIRECTORIES "${TMAC_INCLUDE_DIRS}"
      INTERFACE_LINK_LIBRARIES "${tvm_runtime_LIBRARY}"
      INTERFACE_COMPILE_DEFINITIONS "${TMAC_OBJECT_COMPILE_DEFS}"
      IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
      IMPORTED_OBJECTS "${kernels_OBJECT}"
      INTERFACE_COMPILE_FEATURES cxx_std_17
      POSITION_INDEPENDENT_CODE ON
  )
endif()

if(kernels_LIBRARY AND tvm_runtime_LIBRARY)
  set(TMAC_SHARED_COMPILE_DEFS ${TMAC_COMPILE_DEFS} TMAC_KERNELS_LIBRARY=${kernels_LIBRARY} @TMAC_TVM_COMPILE_DEFS@)
  add_library(t_mac INTERFACE)
  set_target_properties(
    t_mac
    PROPERTIES
      INTERFACE_INCLUDE_DIRECTORIES "${TMAC_INCLUDE_DIRS}"
      INTERFACE_LINK_LIBRARIES "${tvm_runtime_LIBRARY}"
      INTERFACE_COMPILE_DEFINITIONS "${TMAC_SHARED_COMPILE_DEFS}"
      INTERFACE_COMPILE_FEATURES cxx_std_17
      POSITION_INDEPENDENT_CODE ON
  )
endif()

add_library(t_mac_no_tvm INTERFACE)
set_target_properties(
  t_mac_no_tvm
  PROPERTIES
    INTERFACE_INCLUDE_DIRECTORIES "${TMAC_INCLUDE_DIRS}"
    INTERFACE_COMPILE_DEFINITIONS "${TMAC_COMPILE_DEFS}"
    INTERFACE_COMPILE_FEATURES cxx_std_17
    POSITION_INDEPENDENT_CODE ON
)
set(TMAC_KERNELS_SOURCE ${kernels_source})

check_required_components(t_mac)
